package com.gdufe.user.controller;

import java.util.List;

import com.gdufe.user.dto.LoginDto;
import com.gdufe.user.dto.PageUserDto;
import com.gdufe.user.dto.UserDto;
import com.gdufe.user.vo.LoginVo;
import com.gdufe.common.vo.PageBeanVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gdufe.common.entity.User;
import com.gdufe.user.service.UserAdminService;
import com.gdufe.common.utils.R;


/**
 * @author honghu
 * @email 3027708213@qq.com
 * @date 2023-10-15 18:44:14
 */
@RestController
@RequestMapping("/user/admin")
@RequiredArgsConstructor
@Slf4j
public class UserAdminController {

    private final UserAdminService userAdminService;


    /**
     * 根据id查询用户信息
     */
    @GetMapping("/{id}")
    public R adminSelectById(@PathVariable Long id) {
        log.info("根据id查询用户，参数：{}", id);
        User user = userAdminService.getById(id);
        return R.ok().setData(user);
    }

    /**
     * 条件分页查询
     *
     * @param pageUserDto
     * @return
     */
    @GetMapping("/list")
    public R page(PageUserDto pageUserDto) {
        log.info("条件分页查询，参数：{}", pageUserDto);
        PageBeanVo<User> pageRes = userAdminService.pageQuery(pageUserDto);
        return R.ok().setData(pageRes);
    }


    /**
     * 修改用户信息
     */
    @PutMapping
    public R update(@RequestBody UserDto userDto) {
        log.info("修改用户信息，参数：{}", userDto);
        userAdminService.update(userDto);
        return R.ok();
    }

    /**
     * 根据id删除用户信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R deleteById(@PathVariable Long id) {
        log.info("根据id删除用户，参数：{}", id);
        userAdminService.removeById(id);
        return R.ok();
    }

    /**
     * 批量删除用户信息
     */
    @DeleteMapping
    public R deleteBatch(@RequestBody List<Long> ids) {
        log.info("批量删除用户信息，参数：{}", ids);
        userAdminService.removeBatchByIds(ids);
        return R.ok();
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public R login(@RequestBody LoginDto loginDto) {
        log.info("用户登录，参数：{}", loginDto);
        LoginVo loginVo = userAdminService.login(loginDto);
        return R.ok().setData(loginVo);
    }

    /**
     * 用户注册
     *
     * @param userDto
     * @return
     */
    @PostMapping("/register")
    public R register(@RequestBody UserDto userDto) {
        log.info("用户注册，参数：{}", userDto);
        userAdminService.register(userDto);
        return R.ok();
    }

    /**
     * 获取登录的用户信息
     *
     * @return
     */
    @GetMapping("/getUser")
    public R getAdminUser() {
        log.info("获取登录的用户信息...");
        User user = userAdminService.getAdminUser();
        return R.ok().setData(user);
    }

    /**
     * 根据姓名查询用户
     *
     * @param name
     * @return
     */
    @GetMapping("/getUserByName")
    public R getUserByName(@RequestParam("name") String name) {
        User user = userAdminService.getUserByName(name);
        return R.ok().setData(user);
    }
}
